(() => {
    const btn = document.createElement("button")
    document.body.appendChild(btn);
    btn.style.position = "fixed";
    btn.style.right = "200px";
    btn.style.bottom = "200px";
    let stat = false;
    let time;
    const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
    const queue = [];
    const set = ({ tid, userQQ, labels, card }) => {
        card.id = "";
        const ans = labels.findIndex(e => e.className.includes("checked"));
        if (ans == -1) return;
        fetch(`http://114.115.166.23:14520/qq/zlk/${encodeURI(tid)}/${encodeURI(userQQ)}/${ans}/set`)
    }
    const onSubmit = () => {
        if (!queue.length) return;
        const data = queue[0];
        queue.shift();
        set(data);
        onSubmit()
    }
    const get = ({ tid, userQQ, labels, card }) => {
        queue.push({ tid, userQQ, labels, card });
        fetch(`http://114.115.166.23:14520/qq/zlk/${encodeURI(tid)}/get`)
            .then(e => e.json())
            .then((e) => {
                const type = new Array(100).fill(0)
                for (const i of e) type[i]++;
                const spans = labels.map(e => e.children[1]);
                for (let i = 0; i < spans.length; i++) {
                    if (!type[i]) {
                        spans[i].innerText = spans[i].innerText.split("-")[0]
                        continue;
                    }
                    spans[i].innerText = [spans[i].innerText.split("-")[0], type[i]].join("-")
                }
            }).finally(() => {
                fetch("https://ocean.cdposs.qq.com/api/polaris/audit/searchForEventLog", {
                    "headers": {
                        "accept": "application/json",
                        "accept-language": "zh-CN,zh;q=0.9",
                        "content-type": "application/json; charset=utf-8",
                        "queue": "qq_info1",
                        "sec-ch-ua": "\"Chromium\";v=\"122\", \"Not(A:Brand\";v=\"24\", \"Google Chrome\";v=\"122\"",
                        "sec-ch-ua-mobile": "?0",
                        "sec-ch-ua-platform": "\"macOS\"",
                        "sec-fetch-dest": "empty",
                        "sec-fetch-mode": "cors",
                        "sec-fetch-site": "same-origin"
                    },
                    "referrer": "https://ocean.cdposs.qq.com/v2q/qq_info1/search/audit",
                    "referrerPolicy": "strict-origin-when-cross-origin",
                    "body": `{\"queue\":\"qq_info1\",\"version\":\"2024-02-23 14:22:02\",\"tid\":\"${tid}\",\"page\":1,\"page_size\":10,\"order\":0}`,
                    "method": "POST",
                    "mode": "cors",
                    "credentials": "include"
                }).then(e => e.json()).then(e => {
                    if (e.data.some(e => e.event_name.includes("盲审一审"))) {
                        const spans = labels.map(e => e.children[1]);
                        const arr = spans[0].innerText.split("_____");
                        spans[0].innerText = [arr[0], "二审"].join("_____")
                    }
                });
            })
    }
    const start = () => {
        btn.innerText = "暂停"

        time = setInterval(async () => {
            const cards = Array.from(document.getElementsByClassName("ant-card-body")).slice(1);
            const card = cards.find(e => !e.id);
            if (!card) return;
            const a = card.getElementsByTagName("a")[0]
            a.click();
            await sleep(800);
            const popovers = Array.from(document.getElementsByClassName("ant-popover"));
            const popover = popovers.find(e => !e.className.includes("hidden"));
            if (!popover) return;
            const text = popover.innerText.split("\n")
            const tid = text[1]
            const userQQ = text[3];
            card.id = tid;
            const [main, footer] = Array.from(card.children);
            const labels = Array.from(footer.getElementsByTagName("label"));
            a.click()
            get({ tid, userQQ, labels, card })
        }, 1000)
    }
    const submitBtn = document.createElement("button")
    submitBtn.style.position = "fixed"
    submitBtn.style.right = "100px";
    submitBtn.style.bottom = "100px";
    submitBtn.innerText = "提交并保存"
    document.body.appendChild(submitBtn)
    submitBtn.onclick = () => {
        onSubmit()
        Array.from(document.getElementsByTagName("button")).find(e => e.innerText.includes("提交")).click()
    }
    const end = () => {
        btn.innerText = "点击开始"
        clearInterval(time);
    }
    const click = () => {
        stat = !stat
        if (stat) start()
        else end();
    }
    end()
    btn.onclick = click
})()